﻿#region license
// Copyright (c) 2010 Mauricio Scheffer
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//      http://www.apache.org/licenses/LICENSE-2.0
//  
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using System.Linq;
using System.Web.Security;

namespace ProviderInjection {
    public class MyMembershipProvider : MembershipProvider {
        private readonly IUserRepository repository;

        public MyMembershipProvider(IUserRepository repository) {
            this.repository = repository;
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
            var user = new User {
                                    UserName = username,
                                    Password = password,
                                };
            repository.Create(user);
            status = MembershipCreateStatus.Success;
            return new MembershipUser("customProvider", username, Guid.NewGuid(), email, passwordQuestion, "", true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, new DateTime(1980, 1, 1));
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
            return true;
        }

        public override string GetPassword(string username, string answer) {
            return (from u in repository
                   where u.UserName == username
                   select u.Password).First();
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword) {
            var u = repository.First(x => x.UserName == username && x.Password == oldPassword);
            u.Password = newPassword;
            return true;
        }

        public override string ResetPassword(string username, string answer) {
            return Guid.NewGuid().ToString();
        }

        public override void UpdateUser(MembershipUser user) {
        }

        public override bool ValidateUser(string username, string password) {
            return repository.Any(u => u.UserName == username && u.Password == password);
        }

        public override bool UnlockUser(string userName) {
            return true;
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) {
            return null;
        }

        public override MembershipUser GetUser(string username, bool userIsOnline) {
            return null;
        }

        public override string GetUserNameByEmail(string email) {
            return null;
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData) {
            return true;
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline() {
            return 0;
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordRetrieval {
            get { return false; }
        }

        public override bool EnablePasswordReset {
            get { return false; }
        }

        public override bool RequiresQuestionAndAnswer {
            get { return false; }
        }

        public override string ApplicationName { get; set; }

        public override int MaxInvalidPasswordAttempts {
            get { return 10; }
        }

        public override int PasswordAttemptWindow {
            get { return 10; }
        }

        public override bool RequiresUniqueEmail {
            get { return false; }
        }

        public override MembershipPasswordFormat PasswordFormat {
            get { return MembershipPasswordFormat.Clear; }
        }

        public override int MinRequiredPasswordLength {
            get { return 1; }
        }

        public override int MinRequiredNonAlphanumericCharacters {
            get { return 0; }
        }

        public override string PasswordStrengthRegularExpression {
            get { return null; }
        }
    }
}